package com.eportal.action.background;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.action.background.pojo.Page;
import com.eportal.domain.CommunityMsg;
import com.eportal.domain.Sysopt;
import com.eportal.domain.UserMsg;
import com.eportal.service.SysmenuService;
import com.eportal.service.UserMsgService;
import com.eportal.util.ClobUtil;
import com.eportal.util.HomePageConfig;
import com.framework.jqgrid.JqGridPager;
import com.opensymphony.xwork2.ModelDriven;

import net.sf.json.JSONArray;

/**
 * 用户消息管理
 * @author chencheng
 *
 */
public class UserMsgAction extends BaseAction implements ModelDriven<UserMsg> {

    /**
	 * @author chencheng
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(UserMsgAction.class.getName());

    private Map<String, Object> result = new HashMap<String, Object>();

    @Autowired
    UserMsgService userMsgService;
    @Autowired
	private SysmenuService resourceService;
	private static String LISTVIEWURL="/admin/userMsg_listNoticeView.action";
    /**
     * 公告通知
     */
    public String listNoticeView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	getRequest().setAttribute("userRole", userRole);
    	String homePageConfig = HomePageConfig.GONGGAOTZ;
    	String[] str = homePageConfig.split(",");
		getRequest().setAttribute("mpid", Integer.parseInt(str[0]));
		getSession().setAttribute("mpid", Integer.parseInt(str[0]));
		
		Map<String,String> params = new HashMap<String,String>();
		
		Sysopt  user= (Sysopt)getSession().getAttribute("user");
		StringBuilder where = new StringBuilder(" where 1=1 and msg_type<>'3'");
		if (userRole == 21) {
			where.append(" and a.sysoptid =");
			where.append(user.getSid());
		}else if (userRole == 3 || userRole == 4) {
			where.append(" and c.agtcode ='");
			where.append(user.getAgtCode());
			where.append("'");
		}else if (userRole == 5 || userRole == 6) {
    		where.append(" and cm.communityId ='");
    		where.append(user.getCommunityId());
    		where.append("'");
    	}
    	
		Page page = new Page();
		page.setPageSize(10);
		String pageNo = getRequest().getParameter("page");
		if (pageNo != null && pageNo.trim().length() > 0) {
			page.setCurrentPage(Integer.parseInt(pageNo));
		}
		
		page = userMsgService.find(where.toString(), page);
		
		List<Map<String, Object>> buttonMap = resourceService.getButton(LISTVIEWURL);
		getRequest().setAttribute("buttonMap", buttonMap);
		JSONArray json = JSONArray.fromObject(buttonMap);
    	
		getRequest().setAttribute("page", page);
		getRequest().setAttribute("userRole", userRole);
        return SUCCESS;
    }
    
    /**
     * 用户消息管理
     */
    public String NewFile() {
        return SUCCESS;
    }
    
    /**
     * 用户消息管理
     */
    public String listView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	getRequest().setAttribute("userRole", userRole);
    	String homePageConfig = HomePageConfig.GONGGAOTZ;
    	String[] str = homePageConfig.split(",");
		getRequest().setAttribute("mpid", Integer.parseInt(str[0]));
		getSession().setAttribute("mpid", Integer.parseInt(str[0]));
        return SUCCESS;
    }
    
    /**
     * 用户消息列表
     */
    public void page() throws Exception {
        JqGridPager jqGridPager = new JqGridPager();
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	StringBuilder where = new StringBuilder(" 1=1 ");
    	if (userRole == 3 || userRole == 4) {
			where.append(" and c.agtcode ='");
			where.append(user.getAgtCode());
			where.append("'");
		}else if (userRole == 5 || userRole == 6) {
    		where.append(" and cm.communityId ='");
    		where.append(user.getCommunityId());
    		where.append("'");
    	}
    	
    	jqGridPager.setWhere(where.toString());
        setPage(getRequest(), jqGridPager);
        List<Map<String, Object>> userMsg = userMsgService.list(jqGridPager);
        
        jqGridQueryMap(UserMsg.class, getRequest(), userMsg, getResponse(), jqGridPager);
    }
    
    
    /**
     * 公告详情
     */
    public String noticeDetailView(){
    	String sid = getRequest().getParameter("sid");
    	CommunityMsg communityMsg = userMsgService.getCommunityMsg(Long.parseLong(sid));
    	String msg = ClobUtil.ClobToString(communityMsg.getCmsg());
    	getRequest().setAttribute("communityMsg", communityMsg);
    	String receiver = "";
    	if(communityMsg != null && StringUtils.isNotBlank(communityMsg.getReceiver())){
    		receiver = communityMsg.getReceiver().replaceAll("\\|", ",");
    	}
    	getRequest().setAttribute("communityId", communityMsg.getCommunityId());
    	getRequest().setAttribute("receiver", receiver);
    	getRequest().setAttribute("msg", msg);
        return SUCCESS;
    }
    
	/**
     * 消息新增
     */
    public String addView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	if (userRole != 5 && userRole != 6) {
    		return "error";
		}
        return SUCCESS;
    }
    
    /**
     * 消息保存
     */
    public void add(){
    	String range = getRequest().getParameter("range");
    	String receiverId = getRequest().getParameter("receiverId");
    	String cMsg = getRequest().getParameter("cMsg");
    	String abstractMsg = getRequest().getParameter("abstractMsg");
    	result = userMsgService.addMethod(range,receiverId,cMsg,abstractMsg);
        responseBody(result);
    }
    
    /**
     * 消息修改
     */
    public String editView() {
    	String sid = getRequest().getParameter("sid");
    	CommunityMsg communityMsg = userMsgService.getCommunityMsg(Long.parseLong(sid));
    	String msg = ClobUtil.ClobToString(communityMsg.getCmsg());
    	getRequest().setAttribute("communityMsg", communityMsg);
    	String receiver = "";
    	if(communityMsg != null && StringUtils.isNotBlank(communityMsg.getReceiver())){
    		receiver = communityMsg.getReceiver().replaceAll("\\|", ",");
    	}
    	getRequest().setAttribute("receiver", receiver);
    	getRequest().setAttribute("msg", msg);
        return SUCCESS;
    }
    
    
    /**
     * 消息修改保存
     */
    public void update(){
    	String sid = getRequest().getParameter("sid");
    	String range = getRequest().getParameter("range");
    	String receiverId = getRequest().getParameter("receiverId");
    	String cMsg = getRequest().getParameter("cMsg");
    	String abstractMsg = getRequest().getParameter("abstractMsg");
    	result = userMsgService.updateMethod(sid,range,receiverId,cMsg,abstractMsg);
        responseBody(result);
    }

    /**
     * 删除
     */
    public void delete(){
    	String sid = getRequest().getParameter("sid");
    	result = userMsgService.deleteMethod(sid);
        responseBody(result);
    }
    
	private UserMsg userMsg = new UserMsg();

    @Override
    public UserMsg getModel() {
        return userMsg;
    }
}
